package club.haomoon.boss.service.impl.admin;

import club.haomoon.boss.service.admin.IAdminDeptService;
import club.haomoon.core.entity.admin.AdminDeptData;
import club.haomoon.core.mapper.admin.AdminDeptDataMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * -------------------------
 * @Description: 系统部门关联实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/17 17:09
 * -------------------------
 */
@Service
public class AdminDeptServiceImpl implements IAdminDeptService {

    @Resource
    AdminDeptDataMapper adminDeptDataMapper;
    @Override
    public List<Integer> getDeptIdAttr(Integer adminId) {
        List<Integer> ret = new ArrayList<Integer>();
        List<AdminDeptData> rolesList = adminDeptDataMapper.selectList(new QueryWrapper<AdminDeptData>().eq("admin_id", adminId).select("dept_id"));
        if (rolesList.size() > 0) {
            for (AdminDeptData adminDept : rolesList) {
                ret.add(adminDept.getDeptId());
            }
        }
        return ret;
    }

    @Override
    public void batchInsert(Integer adminId, List<Integer> deptIds) {
        this.deleteByAdminId(adminId);
        if (deptIds != null && !deptIds.isEmpty()) {
            deptIds.forEach(item-> {
                this.adminDeptDataMapper.insert(new AdminDeptData(){{
                    setAdminId(adminId);
                    setDeptId(item);
                }});
            });
        }
    }

    @Override
    public void deleteByAdminId(Integer adminId) {
        this.adminDeptDataMapper.delete(new QueryWrapper<AdminDeptData>().eq("admin_id", adminId));
    }
}
